package mine.network;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 上班签到
 * Created by Administrator on 2016/2/24.
 */
public class UserAttend {

    Delegate delegate;
    UserAttendance userAttendance = new UserAttendance();
    ResponseAttend responseAttend = new ResponseAttend();

    public Delegate getDelegate() {
        return delegate;
    }

    public UserAttendance getUserAttendance() {
        return userAttendance;
    }

    public ResponseAttend getResponseAttend() {
        return responseAttend;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUserAttendance(UserAttendance userAttendance) {
        this.userAttendance = userAttendance;
    }

    public void setResponseAttend(ResponseAttend responseAttend) {
        this.responseAttend = responseAttend;
    }

    public void book()
    {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/User/UserAttend";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        responseAttend = gson.fromJson(response,ResponseAttend.class);
                        if (delegate != null)
                            delegate.onResponse(response, UserAttend.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                if (delegate != null)
                    delegate.onErrorResponse(error, UserAttend.this);
            }

        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data", gson.toJson(userAttendance));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public static interface Delegate
    {
        public void onResponse(String response, UserAttend userAttend);
        public void onErrorResponse(VolleyError error, UserAttend userAttend);
    }

    public static class UserAttendance {
        private String attend_Time;
        private String user_ID;
//        private String attend_ComeTime;

        private boolean attend_Type;
        private String attend_Longitude;
        private String attend_Latitude;
        public String getAttend_Time() {
            return attend_Time;
        }
        public void setAttend_Time(String attend_Time) {
            this.attend_Time = attend_Time;
        }
        public String getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }
//        public String getAttend_ComeTime() {
//            return attend_ComeTime;
//        }
//        public void setAttend_ComeTime(String attend_ComeTime) {
//            this.attend_ComeTime = attend_ComeTime;
//        }


        public boolean isAttend_Type() {
            return attend_Type;
        }

        public void setAttend_Type(boolean attend_Type) {
            this.attend_Type = attend_Type;
        }

        public String getAttend_Longitude() {
            return attend_Longitude;
        }

        public void setAttend_Longitude(String attend_Longitude) {
            this.attend_Longitude = attend_Longitude;
        }

        public String getAttend_Latitude() {
            return attend_Latitude;
        }

        public void setAttend_Latitude(String attend_Latitude) {
            this.attend_Latitude = attend_Latitude;
        }


    }

    public static class AttendStatus {
        public static final String FAILED = "0";
        public static final String SUCCESS = "1";
        public static final String ATTENDED = "2";
    }

    public static class ResponseAttend {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }
}
